package com.jhj.common.util;

/**
 * 货币转化工具类
 * 
 * @author GaoXiang Date: 2014-7-16
 */
public class JhjMoneyUtil {
    
    private static JhjMoneyUtil instance = new JhjMoneyUtil();
    
    private JhjMoneyUtil() {
    }
    
    public static JhjMoneyUtil getInstance() {
        return instance;
    }
    
    /**
     * 根据货币金额(单位:分)转化为元的字符串
     * 
     * @param cent
     * @return
     */
    public String getMoneyString(int cent) {
        int yuan = cent / 100;
        int jiao = cent / 10 - yuan * 10;
        int fen = cent - yuan * 100 - jiao * 10;
        return yuan + "." + jiao + fen;
    }
    
    /**
     * 根据优惠价和原价计算折扣的字符串
     * 
     * @param cutPrice 优惠价
     * @param oldPrice 原价
     * @return 折扣字符串  如：5 或者 5.5
     */
    public String getCutRate(int cutPrice, int oldPrice) {
        if (oldPrice == 0) {
            return "";
        }
        long temp = cutPrice * 100L;
        long rate = temp / oldPrice;
        long num = rate /10 ;
        
        if(num>10){
            return "";
        }
        long dot = rate %10 ;
        
        if(dot==0){
            return num+"";
        }
        
        return num+"."+dot;
    }
    
    public static void main(String[] args) {
        JhjMoneyUtil u = JhjMoneyUtil.getInstance();
        String s = u.getCutRate(999999900, 999999999);
        System.out.println(s+"折");
    }
}
